package com.firemoss.magicbus.messaging
{
	import flash.events.Event;
	
	import mx.events.DynamicEvent;

	public class AbstractDefaultMessageConverter implements IMessageConverter
	{
		
		public function get messageAgentMediator() : IMessageAgentMediator
		{
			return _messageAgentMediator
		}
		private var _messageAgentMediator : IMessageAgentMediator
		
		public function AbstractDefaultMessageConverter( messageAgentMediator : IMessageAgentMediator )
		{
			_messageAgentMediator = messageAgentMediator
		}

		public function convertChannelConnectEvent(event:Event) : Object
		{
			if ( messageAgentMediator.connectionStateChangeEvent ) 
			{
				var note : DynamicEvent = new DynamicEvent( messageAgentMediator.connectionStateChangeEvent )
				note.connected = messageAgentMediator.messageAgent.connected
				
				return note
			}
			else
			{
				return null
			}
		}
		
		public function convertAcknowledgeEvent(event:Event) : Object
		{
			if ( messageAgentMediator.acknowledgeEvent ) 
			{
				var note : DynamicEvent = new DynamicEvent( messageAgentMediator.acknowledgeEvent )
				note.cause = event
				
				return note
			}
			else
			{
				return null
			}
		}
		
		public function convertChannelFault(event:Event) : Object
		{
			if ( messageAgentMediator.channelFaultEvent ) 
			{
				var note : DynamicEvent = new DynamicEvent( messageAgentMediator.channelFaultEvent )
				note.cause = event
				
				return note
			}
			else
			{
				return null
			}
		}
		
		public function convertMessageFault(event:Event) : Object
		{
			if ( messageAgentMediator.faultEvent ) 
			{
				var note : DynamicEvent = new DynamicEvent( messageAgentMediator.faultEvent )
				note.cause = event
				
				return note
			}
			else
			{
				return null
			}
		}
		
	}
}